const crypto = require('crypto');
/**验签sign参数加密
 * @param $data
 * @param $appSecret
 * @return string
 */
const makeSign = ($data, $appSecret) => {
  // 一个crypto只能调用一次，所以放在内部初始化
  let md5 = crypto.createHash('md5');
  let $str = '';
  let $index = 0;
  let $sortPor = [];
  for (let key in $data) {
    $sortPor.push(`${key}=${$data[key]}`);
  }
  // 排序
  $sortPor.sort();
  // 转url
  for (let key in $sortPor) {
    $str = `${$str}${$index === 0 ? '' : '&'}${$sortPor[key]}`;
    $index++;
  }
  // md5加密
  let $ret = md5.update(`${$str}&key=${$appSecret}`).digest('hex');
  return $ret;
}
const requestDtk = (signData) => {
  const appSecret = '6891b277c853730617c40d08e5f5001c'; //应用的Secret 
  const data = {
    appKey: '5cfbd0d8455f1', // 应用分配的appKey
    ...signData
  };
  // let urlList = {
  //   'hotSearch': 'https://openapi.dataoke.com/api/category/get-top100', // 热搜记录
  //   'itemList': 'https://openapi.dataoke.com/api/goods/get-goods-list', // 商品列表
  // }
  // let options = {
  //   url: `${urlList[apiUrl]}?appKey=${data.appKey}&version=${data.version}&sign=${makeSign(data,appSecret)}`, //请求地址
  //   method: 'GET',
  // };
  let options = {
    appKey: data.appKey,
    version: data.version,
    sign: makeSign(data,appSecret),
  }
  return options;
}
// getCategoryTop100();
export default {
  requestDtk
}
